package com.example.shop_2304

import android.content.Intent
import android.os.Bundle
import android.view.KeyEvent
import android.widget.Toast
import androidx.activity.enableEdgeToEdge
import androidx.appcompat.app.AppCompatActivity
import androidx.core.view.ViewCompat
import androidx.core.view.WindowInsetsCompat
import androidx.navigation.findNavController
import androidx.navigation.fragment.NavHostFragment
import androidx.navigation.ui.setupWithNavController
import com.alibaba.android.arouter.facade.Postcard
import com.alibaba.android.arouter.facade.annotation.Autowired
import com.alibaba.android.arouter.facade.annotation.Route
import com.alibaba.android.arouter.facade.callback.NavigationCallback
import com.alibaba.android.arouter.launcher.ARouter
import com.blankj.utilcode.util.SPUtils
import com.blankj.utilcode.util.ToastUtils
import com.example.base.mvvm.BaseActivity
import com.example.base.mvvm.BaseViewActivity
import com.example.shop_2304.databinding.ActivityMainBinding
import com.example.shop_2304.login.UpdateActivity
import com.example.shop_2304.welfg.BlankFragment

@Route(path = "/app/MainActivity")
class MainActivity : BaseViewActivity<ActivityMainBinding>() {

    @Autowired
    @JvmField
    var index = 0
    override fun initView() {
        ARouter.getInstance().inject(this)
        //找到fragment
      /*  val navHostFragment =
            supportFragmentManager.findFragmentById(R.id.container) as NavHostFragment
        //找到控制器
        val navController = navHostFragment.navController
        //和bottom联动
        binding.bottom.setupWithNavController(navController)*/
        binding.bottom.setupWithNavController(findNavController(R.id.container))

        requestPermissions(arrayOf("android.permission.WRITE_EXTERNAL_STORAGE","android.permission.READ_EXTERNAL_STORAGE"),103)

        when(index){
            0-> binding.bottom.selectedItemId = R.id.homeFragment
            1-> binding.bottom.selectedItemId = R.id.carFragment
            2-> binding.bottom.selectedItemId = R.id.mineFragment
            3-> binding.bottom.selectedItemId = R.id.car2Fragment
        }


        /* binding.btn.setOnClickListener {
             ARouter.getInstance().build("/app/UpdateActivity").navigation()*/
            //startActivity(Intent(this,UpdateActivity::class.java))
            //跳转到fragment
            //val blankFragment = ARouter.getInstance().build("/app/BlankFragment").navigation(this) as BlankFragment
            //期望有返回值  在页面中重写onActivityResult来回写数据
            //ARouter.getInstance().build("/app/UpdateActivity").navigation(this,102)
            //考虑到生命周期的范围，可能会导致再次跳转时，不能跳转成功，可在回调函数中去结束进程
           /* ARouter.getInstance().build("/app/UpdateActivity").navigation(this,object:NavigationCallback{
                override fun onFound(postcard: Postcard?) {
                    TODO("Not yet implemented")
                }

                override fun onLost(postcard: Postcard?) {
                    TODO("Not yet implemented")
                }

                override fun onArrival(postcard: Postcard?) {
                    finish()
                }

                override fun onInterrupt(postcard: Postcard?) {
                    TODO("Not yet implemented")
                }
            })*/
     /*   }*/
    }

    override fun initData() {

    }

    override fun getLayout(): Int  = R.layout.activity_main

    //退出  记录退出时间
    override fun onKeyDown(keyCode: Int, event: KeyEvent?): Boolean {
        if(keyCode==KeyEvent.KEYCODE_BACK){
            //保存上次退出时间
            SPUtils.getInstance().put("time",System.currentTimeMillis())
        }
        return super.onKeyDown(keyCode, event)
    }

}